package validation;

import java.util.Hashtable;

import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Isattribute implements Validation 
{
	DBManager database;
	String TYPE="type";
	GetIdName gid;
	String ATTRIBUTE="attribute";
	String value;
	
	public String getMessage()
	{
		return  "master type is wrong";
	}

	public void setDbmanager(DBManager database) 
	{
		this.database = database;
		gid = new GetIdName(database);
		TYPE = gid.getId(TYPE); 
	//	ATTRIBUTE = gid.getId(ATTRIBUTE);
	}

	public boolean validate(String Value)	
	{
		if(Value.toLowerCase().equals(ATTRIBUTE.toLowerCase()))
			return true;
		Value = gid.getId(Value);
		//String MASTERPATH = Input.MASTERREPORTSPATH + Value + "//" + Value + "_mid" + Input.FILRFORMAT;
		Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		table.put("pid", TYPE);
		table.put("mid",Value);
		/*SearchRecord sr = new SearchRecord(MASTERPATH);
		sr.fileOpen();
		String line[] = sr.getRecordSet(tabel);
		sr.fileClose();*/
		table.put("td","null");
		RecordSearch ps = new RecordSearch(database);
		ps.setConditionMap(table);
		String line[] = ps.getArrayFromResultSet();
		String mastertype="";
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			mastertype = splitline[Column.pv_index-1];
			if(mastertype.equals("0"))
			{
				mastertype = splitline[Column.vt_index-1];
				//mastertype = gid.getId(mastertype)
			}
			else
				mastertype = gid.getItem(mastertype);
			if(mastertype.toLowerCase().equals(ATTRIBUTE.toLowerCase()))
			{
		    	 return true;
			}
				
			else
			{
		    		return validate(mastertype);
			}
			
		}
		return false;
	}

	public boolean validate(String Value, String ConditionValue) 
	{
		return false;				
	}

	public boolean validate(String Value, String ConditionValue, String Refvalue) 
	{
		/*this.value = Value;
		boolean tempflag = validate(Value);
		if(ConditionValue.toLowerCase().equals("true"))
			return !tempflag;
		else
			return tempflag;*/
		if(ConditionValue.equalsIgnoreCase("true"))
		{
			return validate(Value);
		}
		else if(ConditionValue.equalsIgnoreCase("false"))
		{
	    		return true;
		}
		
		else
		{
	    		return false;
		}
		
	}

	public void setBasicvalue(String value) 
	{
		
	}
	
	public static void main(String[] args) 
	{
		DBManager sqldb = new DBManager();
		sqldb.getConnect();
		Isattribute atr = new Isattribute();
		atr.setDbmanager(sqldb);
		System.out.println(atr.validate("quantity"));
		
	}

}
